﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
namespace Common {
    public class GLUtility {
        public unsafe static int getIntFromPos(byte[] header, int index) {
            fixed (byte* pHeader = header) {
                //int* pInt = (int*)(pHeader + index);
                //var value = *pInt;
                //var i0 = (long)*(pHeader+index);
                //var i1 = (long)*(pHeader +index+ 1)<<8;
                //var i2 = (long )* (pHeader+index + 2) << 16;
                //var i3 = (long)*(pHeader+index + 3) << 24;
                var sum = ((uint)*(pHeader + index)) |
                    ((uint)*(pHeader + index + 1) << 8) |
                    ((uint)*(pHeader + index + 2) << 16) |
                    ((uint)*(pHeader + index + 3) << 24);
                return unchecked((int)sum);
            }
        }
        public static unsafe int SizeOf<T>() where T : unmanaged {
            return sizeof(T);
        }
        public static float ToRadians(float value) {
            //double rate = Math.PI / 180;
            return (float)(Math.PI * value / 180);
        }
        public static float NormalizeRadians(float value) {
            var result = value % (2 * Math.PI);
            return (float)result;
        }
        public static Vector3[] ToVector3Array(float[] data) {
            if (data.Length % 3 != 0) {
                throw new Exception("input data length must be divisible by 3");
            }
            Vector3[] vertexs = new Vector3[data.Length / 3];
            for (int i = 0; i < vertexs.Length; i++) {
                int offset = i * 3;
                vertexs[i].X = data[offset++];
                vertexs[i].Y = data[offset++];
                vertexs[i].Z = data[offset++];
            }
            return vertexs;
        }
        public static Vector2[] ToVector2Array(float[] data) {
            if (data.Length % 3 != 0) {
                throw new Exception("input data length must be divisible by 2");
            }
            Vector2[] vertexs = new Vector2[data.Length / 2];
            for (int i = 0; i < vertexs.Length; i++) {
                int offset = i * 2;
                vertexs[i].X = data[offset++];
                vertexs[i].Y = data[offset++];
            }
            return vertexs;
        }
    }
}
